from urllib import request,parse
import json
#0、参照老师课堂代码做如下修改
#url = "http://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule"
				#1、在有道翻译原地址上去掉“_o”
url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
t = {}
#t['i']= 'hello'
t['i']= input("请输入要翻译的单词:")
#2、在请求参数里面注明‘doctype’=‘json’
t['doctype']='json'

translateResult = parse.urlencode(t)
headers = {'Content-Length':len(translateResult)}
req = request.Request(url,data=bytes(translateResult,encoding="utf-8"),headers=headers)
res = request.urlopen(req)
str_json = res.read().decode("utf-8")
myjson = json.loads(str_json)

#3、观察网页返回数据格式，以如下格式输出
print("翻译结果为:"+myjson['translateResult'][0][0]['tgt'])